#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>

typedef std::vector<int>::iterator ITERATOR;

bool next_perm(ITERATOR f, ITERATOR l) {
  if (f == l)
    return false;

  ITERATOR i = f;
  ++i;
  if (i == l)
    return false;

  i = l;
  --i;

  for(;;) {
    ITERATOR ii = i;
    --i;
    if ( *i< *ii ) {
      ITERATOR j = l;
      while ( *i >= *--j ) {}
      iter_swap(i, j);
      reverse(ii, l);
      return true;
    }
    if (i == f) {
      reverse(f, l);
      return false;
    }
  }
}

int main(){
	int V[] = {1,2,3,4};
	std::vector<int> v;
	v.assign(V,V+sizeof(V)/sizeof(V[0]));
	std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout," "));
	std::cout<<std::endl;
	while(next_perm(v.begin(),v.end())) {
			std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout," "));
			std::cout<<std::endl;
	}

}
